package org.jeecg.modules.online.graphreport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.online.graphreport.entity.OnlGraphreportParams;
import org.jeecg.modules.online.graphreport.service.IOnlGraphreportParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: onl_graphreport_params
 * @Author: jeecg-boot
 * @Date: 2021-04-08
 * @Version: V1.0
 */
//@Api(tags = "onl_graphreport_params")
@RestController("onlGraphreportParamsController")
@RequestMapping("/online/graphreport/params")
@Slf4j
public class OnlGraphreportParamsController extends JeecgController<OnlGraphreportParams, IOnlGraphreportParamsService> {

    @Autowired
    private IOnlGraphreportParamsService paramsService;

    /**
     * 通过 headId 查询参数列表
     *
     * @param headId
     * @return
     */
    @GetMapping(value = "/listByHeadId")
    public Result<?> listByHeadId(@RequestParam(name = "headId") String headId) {
        List<OnlGraphreportParams> paramsList = paramsService.selectByMainId(headId);
        return Result.OK(paramsList);
    }

    /**
     * 获取图表参数
     */
    @GetMapping(value = "/getParamsInfo/{code}")
    public Result<?> getParamsInfo(@PathVariable("code") String code) {
        return this.listByHeadId(code);
    }

    /**
     * 分页列表查询
     *
     * @param onlGraphreportParams
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-分页列表查询")
    //@ApiOperation(value = "onl_graphreport_params-分页列表查询", notes = "onl_graphreport_params-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            OnlGraphreportParams onlGraphreportParams,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req
    ) {
        QueryWrapper<OnlGraphreportParams> queryWrapper = QueryGenerator.initQueryWrapper(onlGraphreportParams, req.getParameterMap());
        Page<OnlGraphreportParams> page = new Page<>(pageNo, pageSize);
        IPage<OnlGraphreportParams> pageList = paramsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param onlGraphreportParams
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-添加")
    //@ApiOperation(value = "onl_graphreport_params-添加", notes = "onl_graphreport_params-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody OnlGraphreportParams onlGraphreportParams) {
        paramsService.save(onlGraphreportParams);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param onlGraphreportParams
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-编辑")
    //@ApiOperation(value = "onl_graphreport_params-编辑", notes = "onl_graphreport_params-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody OnlGraphreportParams onlGraphreportParams) {
        paramsService.updateById(onlGraphreportParams);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-通过id删除")
    //@ApiOperation(value = "onl_graphreport_params-通过id删除", notes = "onl_graphreport_params-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        paramsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-批量删除")
    //@ApiOperation(value = "onl_graphreport_params-批量删除", notes = "onl_graphreport_params-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.paramsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "onl_graphreport_params-通过id查询")
    //@ApiOperation(value = "onl_graphreport_params-通过id查询", notes = "onl_graphreport_params-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        OnlGraphreportParams onlGraphreportParams = paramsService.getById(id);
        if (onlGraphreportParams == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(onlGraphreportParams);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param onlGraphreportParams
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OnlGraphreportParams onlGraphreportParams) {
        return super.exportXls(request, onlGraphreportParams, OnlGraphreportParams.class, "onl_graphreport_params");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OnlGraphreportParams.class);
    }

}
